package gold.contest;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5993 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int findFinalValue(int[] nums, int original) {
            Arrays.sort(nums);
            for (int num : nums) {
                if (num == original) original *= 2;
            }

            return original;
        }
    }

    class Solution_Shit {
        public int findFinalValue(int[] nums, int original) {
            double curMaxFactor = 0.5;
            boolean isFind = false;
            for (int num : nums) {
                if (num % original == 0 && nCF(num / original))
                    curMaxFactor = Math.max(num / original, curMaxFactor);
                isFind = true;
            }

            return (int) (curMaxFactor * original) * (isFind ? 2 : 1);
        }

        public boolean nCF(int n) {
            if (1 == n) return true;
            boolean b = false;
            while (true) {
                int j = n % 2;
                n = n / 2;
                if (j == 1) {
                    b = false;
                    break;
                }
                if (n == 2) {
                    b = true;
                    break;
                }

            }
            return b;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.findFinalValue(InputUtil.toIntegerArray("[5,3,6,1,12]"), 3));
        System.out.println(solution.findFinalValue(InputUtil.toIntegerArray("[2,7,9]"), 4));
        System.out.println(solution.findFinalValue(InputUtil.toIntegerArray("[2]"), 2));
        System.out.println(solution.findFinalValue(InputUtil.toIntegerArray("[1,16,13,19,12,10]"), 2));
    }

    public static void main(String[] args) throws Exception {
        LC5993 an = new LC5993();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
